package com.hs.sfzy.controller;

import com.hs.sfzy.entity.UserCourse;
import com.hs.sfzy.service.UserCourseService;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserCourseController
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年10月29日 14:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/userCourse")
public class UserCourseController {

    @Autowired
    UserCourseService userCourseService;

    @DeleteMapping("/{id}")
    public Object deleteById(@PathVariable(required = false) Long id){
        if (id == null){
            return ResultUtil.fail("id有误");
        }
        int num = userCourseService.deleteUserCourse(id);
        if (num > 0){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.fail("删除失败");
        }
    }

    @PostMapping("/add")
    public Result insert(@RequestBody UserCourse userCourse){
        int num = userCourseService.insert(userCourse);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("/del")
    public Result deleteByCourseIdAndUserId(@RequestBody UserCourse userCourse){
        int num = userCourseService.deleteByCourseIdAndUserId(userCourse);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
